﻿@model TopicModel
@using Nop.Web.Models.Topics;
@if (Model.IsPasswordProtected)
{
    <script type="text/javascript">
        $(function () {
            $('#button-password-@Model.Id').click(function () {
                var topicId = $("#@Html.FieldIdFor(model => model.Id)").val();
                var password = $('#password-@Model.Id').val();
                $.ajax({
                    cache: false,
                    type: 'POST',
                    url: '@Url.RouteUrl("TopicAuthenticate")',
                    data: { "id": topicId, "password": password },
                    dataType: 'json',
                    success: function (data) {
                        if (data.Authenticated) {
                            $('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').html(data.Title);
                            if ($('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').text().length == 0) {
                                $('#ph-title-@Model.Id').hide();
                            }
                            $('#ph-topic-@Model.Id .topic-html-content-body').html(data.Body);
                            $('#ph-password-@Model.Id').hide();
                            $('#ph-topic-@Model.Id').show();
                        }
                        else {
                            $('#password-error-@Model.Id').text(data.Error);
                            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
                        }
                    }
                });
                return false;
            });
        });

        $(document).ready(function () {
            $('#ph-topic-@Model.Id').hide();
            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
        });
    </script>
    <div class="topic-password" id="ph-password-@Model.Id">
        @using (Html.BeginRouteForm("TopicAuthenticate"))
        {
            @Html.HiddenFor(model => model.Id)
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { id = "password-" + @Model.Id })
                <input type="submit" id="button-password-@Model.Id" value="@T("Topic.Button")" class="button-1 topic-password-button" />
            </div>
            <div class="password-error">
                <span id="password-error-@Model.Id"></span>
            </div>
        }
    </div>
    <div class="topic-html-content" id="ph-topic-@Model.Id">
        <div id="ph-title-@Model.Id">
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @Model.Title</h2>
            </div>
        </div>
        <div class="topic-html-content-body">
            @Html.Raw(Model.Body)
        </div>
    </div>
}
else
{
    <div class="topic-html-content">
        @if (!String.IsNullOrEmpty(Model.Title))
        {
            <div class="topic-html-content-title">
                <h2 class="topic-html-content-header">
                    @Model.Title</h2>
            </div>
        }
        <div class="topic-html-content-body">
            @Html.Raw(Model.Body)
        </div>
    </div>
}